using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Text;
using System.Windows.Forms;

using Entities;
using Bll;

namespace AdvancedThreeTier
{
    public partial class Form1 : Form
    {
        CategoryBll cateBll = new CategoryBll();

        public Form1()
        {
            InitializeComponent();
        }

        private void AddItem(CategoryInfo cate)
        {
            ListViewItem lvItem = new ListViewItem(cate.CategoryID.ToString());
            lvItem.SubItems.Add(cate.CategoryName);
            lvItem.SubItems.Add(cate.Description);
            lvItem.Tag = cate;
            lstCategories.Items.Add(lvItem);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            IList<CategoryInfo> categories = cateBll.GetAll();

            // Display the list of Categories to the ListView named lstCategories
            foreach (CategoryInfo cate in categories)
            {
                ListViewItem lvItem = new ListViewItem(cate.CategoryID.ToString());
                lvItem.SubItems.Add(cate.CategoryName);
                lvItem.SubItems.Add(cate.Description);
                lvItem.Tag = cate;
                lstCategories.Items.Add(lvItem);
            }
        }

        private void lstCategories_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lstCategories.SelectedItems.Count > 0)
            {
                ListViewItem lvItem = lstCategories.SelectedItems[0];
                CategoryInfo cateObj = lvItem.Tag as CategoryInfo;
                if (cateObj != null)
                {
                    txtCategoryID.Text = cateObj.CategoryID.ToString();
                    txtCategoryName.Text = cateObj.CategoryName;
                    rtxtDescription.Text = cateObj.Description;
                    picture.Image = cateObj.Picture;
                }
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            CategoryInfo cate = new CategoryInfo();
            cate.CategoryName = txtCategoryName.Text;
            cate.Description = rtxtDescription.Text;
            cate.Picture = picture.Image;
            if (cateBll.Insert(cate))
                AddItem(cate);
        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog1 = new OpenFileDialog();

            //openFileDialog1.InitialDirectory = "c:\\";
            openFileDialog1.Filter = "Image files (*.jpg)|*.jpg|All files (*.*)|*.*";
            openFileDialog1.FilterIndex = 2;
            openFileDialog1.RestoreDirectory = true;
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                picture.ImageLocation = openFileDialog1.FileName;
            }

        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if (lstCategories.SelectedItems.Count > 0)
            {
                ListViewItem lvItem = lstCategories.SelectedItems[0];

                CategoryInfo cateObj = new CategoryInfo();
                cateObj.CategoryID = (lvItem.Tag as CategoryInfo).CategoryID;
                cateObj.CategoryName = txtCategoryName.Text;
                cateObj.Description = rtxtDescription.Text;
                cateObj.Picture = picture.Image;
                if (cateBll.Update(cateObj))
                {
                    // update data to database successful, so update data on the UI
                    lvItem.SubItems[1].Text = cateObj.CategoryName;
                    lvItem.SubItems[2].Text = cateObj.Description;
                    lvItem.Tag = cateObj;
                }
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (lstCategories.SelectedItems.Count > 0)
            {
                ListViewItem lvItem = lstCategories.SelectedItems[0];

                CategoryInfo cateObj = lvItem.Tag as CategoryInfo;
                if (cateBll.Delete(cateObj))
                {
                    // Delete data from database successful, so remove ListViewItem from ListView
                    lstCategories.Items.Remove(lvItem);
                }
            }
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
    }
}